package com.dajiba.demo;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.SizeUtils;

import java.util.ArrayList;

public class CustomNineView extends ViewGroup {

    ArrayList<String> datas = new ArrayList<>();

    public CustomNineView(Context context) {
        super(context);
        for (int i = 0; i < 5; i++) {
            datas.add("");
            ImageView imageView = new ImageView(context);
            addView(imageView);
        }
    }

    public CustomNineView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomNineView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    //子view宽高
    int mChildViewWidth = 0;
    int mChildViewHeight = mChildViewWidth;

    //上下间隔
    int mVerticalInterval = SizeUtils.dp2px(5);
    int mHorizontalInterval = SizeUtils.dp2px(5);

    //横向个数
    int mHorizontalSum = 3;

    LayoutParams mChildLayoutParams = new LayoutParams(mChildViewWidth, mChildViewWidth);

    public void upData(ArrayList<String> strings) {
        removeAllViews();
        datas.clear();
        datas.addAll(strings);
        for (int i = 0; i < strings.size(); i++) {
            ImageView imageView = new ImageView(getContext());
            addView(imageView);
        }
        requestLayout();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        measureChildren(widthMeasureSpec, heightMeasureSpec);
        int specWidthSize = MeasureSpec.getSize(widthMeasureSpec);
        int specWidthMode = MeasureSpec.getMode(widthMeasureSpec);

        int specHeightSize = MeasureSpec.getSize(heightMeasureSpec);
        int specHeightMode = MeasureSpec.getMode(heightMeasureSpec);

        //算出横向item宽度
        //横向间隔宽度
        int total = mHorizontalInterval * (mHorizontalSum - 1);
        //item宽度
        mChildViewWidth = (specWidthSize - total) / mHorizontalSum;
        //高度
        if (getChildCount() <= 3) {
            specHeightSize = mChildViewWidth;

        } else if (getChildCount() <= 6) {
            specHeightSize = mChildViewWidth * 2;

        } else if (getChildCount() <= 9) {
            specHeightSize = mChildViewWidth * 3;
        }
        setMeasuredDimension(specWidthSize, specHeightSize);
        setBackgroundColor(Color.BLUE);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int usedHorizontal = 0;//水平已经使用的距离
        int usedVertical = 0;//垂直已经使用的距离
        int width = getMeasuredWidth();
        for (int i = 0; i < getChildCount(); i++) {
            View view = getChildAt(i);
            mChildLayoutParams.width = mChildViewWidth;
            mChildLayoutParams.height = mChildViewWidth;
            view.setLayoutParams(mChildLayoutParams);
            view.setBackgroundColor(Color.BLACK);
            //判断是否要换行
            if (mChildViewWidth + usedHorizontal > width || (getChildCount() == 4 && i == 2)) {
                usedVertical = usedVertical + mChildViewWidth + mVerticalInterval;
                usedHorizontal = 0;
            }
            view.layout(usedHorizontal, usedVertical, usedHorizontal + mChildViewWidth, usedVertical + mChildViewWidth);
            usedHorizontal = usedHorizontal + mHorizontalInterval + mChildViewWidth;
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }
}
